package cn.exrick.xboot.modules.oauthClient.controller;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.oauthClient.entity.OauthClientDetails;
import cn.exrick.xboot.modules.oauthClient.service.IOauthClientDetailsService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author portal
 */
@Slf4j
@RestController
@Api(description = "Oauth客户端管理管理接口")
@RequestMapping("/xboot/oauthClientDetails")
@Transactional
public class OauthClientDetailsController {

    @Autowired
    private IOauthClientDetailsService iOauthClientDetailsService;

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<IPage<OauthClientDetails>> select(@ModelAttribute Page page,String key){
        OauthClientDetails details = new OauthClientDetails();
        details.setClientId(key);
        IPage<OauthClientDetails> oauthClientDetails = iOauthClientDetailsService.slectByKey(page,details);
        return new ResultUtil<IPage<OauthClientDetails>>().setData(oauthClientDetails);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<OauthClientDetails>> getAll(){

        List<OauthClientDetails> list = iOauthClientDetailsService.list();
        return new ResultUtil<List<OauthClientDetails>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<OauthClientDetails>> getByPage(@ModelAttribute PageVo page){

        IPage<OauthClientDetails> data = iOauthClientDetailsService.page(PageUtil.initMpPage(page));
        return new ResultUtil<IPage<OauthClientDetails>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<OauthClientDetails> saveOrUpdate(@Valid OauthClientDetails oauthClientDetails , BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return new ResultUtil<OauthClientDetails>().setErrorMsg("所需参数不能为空");
        }
        OauthClientDetails newDetails = iOauthClientDetailsService.getByClientId(oauthClientDetails.getClientId());
        OauthClientDetails oldDetails = iOauthClientDetailsService.getByOldClientId(oauthClientDetails.getOldClientId());
        if (newDetails == null && oldDetails == null){
            oauthClientDetails.setClientSecret(new BCryptPasswordEncoder().encode(oauthClientDetails.getClientSecret()));
            iOauthClientDetailsService.saveClient(oauthClientDetails);
            return new ResultUtil<OauthClientDetails>().setData(oauthClientDetails);
        }else {
            //判断密钥是否是已经加密过的
            if (!oauthClientDetails.getClientSecret().equals(oldDetails.getClientSecret())){
                oauthClientDetails.setClientSecret(new BCryptPasswordEncoder().encode(oauthClientDetails.getClientSecret()));
            }
            iOauthClientDetailsService.updateClent(oauthClientDetails);
            return new ResultUtil<OauthClientDetails>().setData(oauthClientDetails);
        }
    }


    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@PathVariable String[] ids){

        for(String id : ids){
            iOauthClientDetailsService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }

    /**
     * @author    : LiYuanzhuo
     * description: 判断系统标识
     * create time: 2019/12/13 16:36
     *//*
    public String verdictSys(OauthClientDetails details){
        String client_id = details.getClientId();
        String description = null;
        switch (client_id) {
            case CommonConstant.FD_CLIENT:
                description = "财务系统";
                break;
            case CommonConstant.HR_CLIENT:
                description = "人力系统";
                break;
            case CommonConstant.OA_CLIENT:
                description = "OA系统";
                break;
            case CommonConstant.ED_CLIENT:
                description = "OA系统";
                break;
            case CommonConstant.PM_CLIENT:
                description = "OA系统";
                break;
            default:
                description = "未知系统";
                break;
        }

        return description;
    }*/
}
